package com.blockchain.securewallet.activities;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.blockchain.securewallet.R;
import com.blockchain.securewallet.WalletApp;
import com.blockchain.securewallet.base.BaseActivity;
import com.blockchain.securewallet.model.db.CoinWallet;
import com.blockchain.securewallet.utils.ToastUtils;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bingoogolapple.qrcode.core.BGAQRCodeUtil;
import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;

public class QueryAccountQrCodeActivity extends BaseActivity {

    public static final String KEY_MODULE_FROM = "key_module_from";
    public static final String KEY_WALLET_ID = "key_wallet_id";
    public static final int MODULE_SCAN = 1;
    public static final int MODULE_WALLET = 0;

    @BindView(R.id.cv_qr_code)
    ConstraintLayout cvQrCode;

    @BindView(R.id.iv_qr_code)
    ImageView ivQrCode;

    @BindView(R.id.iv_warn_hint)
    ImageView ivWarnHint;

    @BindView(R.id.tv_warn_hint)
    TextView tvWarnHint;

    @BindView(R.id.tv_warn_step)
    TextView tvStepOne;

    @BindView(R.id.tv_warn_hint_next)
    TextView tvWarnHintNext;

    @BindView(R.id.btn_next)
    Button btnNext;

    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.cl_normal)
    TextView cl_normal;

    @BindView(R.id.cl_mismatchting)
    ConstraintLayout cl_mismatchting;

    private int module_from = 0;
    private int walletId;
    private CoinWallet wallet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_query_account_qr_code);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        if (intent!=null) {
            module_from = intent.getIntExtra(KEY_MODULE_FROM,MODULE_WALLET);
            walletId =  intent.getIntExtra(KEY_WALLET_ID,0);
        }
        wallet = WalletApp.getInstance().queryCurrrentWallet().get(walletId);
//        wallet = WalletApp.getInstance().getDaoSession().load(CoinWallet.class,walletId);
        if(wallet.getType()==CoinWallet.TYPE_BIT_COIN) {
            tvTitle.setText(R.string.bit_coin_name);
        } else if (wallet.getType()==CoinWallet.TYPE_ETH_COIN){
            tvTitle.setText(R.string.eth_coin_name);
        } else if (wallet.getType()==CoinWallet.TYPE_LTC_COIN) {
            tvTitle.setText(R.string.ltc_coin_name);
        } else if (wallet.getType()==CoinWallet.TYPE_BCH_COIN) {
            tvTitle.setText(R.string.bch_coin_name);
        }else if (wallet.getType()==CoinWallet.TYPE_EOS_COIN) {
            tvTitle.setText(R.string.eos_coin_name);
        }

        if (module_from == MODULE_WALLET) {
            //正常的情况，显示TextView
            cl_normal.setVisibility(View.VISIBLE);
            cl_mismatchting.setVisibility(View.GONE);

        } else {
            //地址不一致的情况
            ToastUtils.showToast("该二维码地址与当前钱包地址不一致");

            /*ivWarnHint.setVisibility(View.INVISIBLE);
            tvWarnHint.setVisibility(View.INVISIBLE);
            tvStepOne.setVisibility(View.INVISIBLE);*/

            cl_normal.setVisibility(View.GONE);
            cl_mismatchting.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        createChineseQRCode();
    }

    private void createChineseQRCode() {
        new AsyncTask<Void, Void, Bitmap>() {
            @Override
            protected Bitmap doInBackground(Void... params) {
                JSONObject object = new JSONObject();
                String info = wallet.getAddress();
                try {
                    object.put("id", WalletApp.queryCurrrentAccountId());
                    object.put("t", 2);
                    object.put("ct", wallet.getType());
                    object.put("a", wallet.getAddress());
                    info = object.toString();
                } catch (Exception e) {

                }
                return QRCodeEncoder.syncEncodeQRCode(info, BGAQRCodeUtil.dp2px(QueryAccountQrCodeActivity.this, 200));
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                if (bitmap != null) {
                    ivQrCode.setImageBitmap(bitmap);
                    cvQrCode.setBackgroundColor(getResources().getColor(R.color.normal_text));
                } else {
                    Toast.makeText(QueryAccountQrCodeActivity.this, "生成中文二维码失败", Toast.LENGTH_SHORT).show();
                }
            }
        }.execute();
    }

    @OnClick(R.id.btn_back)
    void back() {
        finish();
    }

    @OnClick(R.id.btn_next)
    void next() {
        Intent intent = new Intent();
        intent.setClass(this, QrCodeScanActivity.class);
        intent.putExtra(QrCodeScanActivity.KEY_WALLET_ID, wallet.getId());

        if (module_from == MODULE_WALLET) {
            //正常的情况
            intent.putExtra(QrCodeScanActivity.KEY_MODULE_FROM, QrCodeScanActivity.MODULE_UPDATE_WALLET);
        } else {
            //错误的情况
            intent.putExtra(QrCodeScanActivity.KEY_MODULE_FROM, QrCodeScanActivity.MODULE_UPDATE_ERROE);
        }
       
        startActivity(intent);
        finish();
    }

}
